package com.hxd.mall.module.product.search.history

import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.hxd.mall.R
import com.hxd.mall.base.BaseInjectFragment
import com.hxd.mall.constant.HxdConstantsAccount.localSearch
import com.hxd.mall.constant.HxdConstantsAccount.localSearchKey
import com.hxd.mall.module.product.search.SearchActivity
import com.hxd.mall.module.product.search.bean.HotSearchBean
import com.hxd.mall.utils.local.HxdSharePreferenceUtil.deleteData
import com.hxd.mall.utils.local.HxdSharePreferenceUtil.getStringData
import com.hxd.mall.utils.open.GsonUtil
import com.hxd.mall.utils.open.ToastUtil
import com.hxd.mall.utils.open.addSetOnClickListener
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import kotlinx.android.synthetic.main.fragment_search_history_layout.*

/**
 * @author zhaoyong
 * @description: 搜索历史
 * @date :2019-10-22 10:50
 */
class SearchHistoryFragment : BaseInjectFragment<ISearchHistoryPresenter>(), ISearchHistoryView {

    private var mBean: HotSearchBean? = null

    override fun attachLayoutRes(): Int = R.layout.fragment_search_history_layout

    override fun initViews() {
        isReload = true

        localSearch = GsonUtil.parseList(getStringData(localSearchKey)) ?: localSearch
        if (localSearch.size != 0) {
            showLocalSearch()
        }

        everyoneSearch.setOnTagClickListener { _, position, _ ->
            // showSearchRecord(mBean?.data?.hotWords?.get(position))
            mBean?.data?.hotWords?.get(position)?.let { (activity as SearchActivity).toSearch(it) }
            false
        }
        mPresenter?.getData()
    }

    //展示本地搜索数据
    private fun showLocalSearch() {
        local_search_layout.visibility = View.VISIBLE
        val localAdapter = object : TagAdapter<String>(localSearch) {
            override fun getView(parent: FlowLayout?, position: Int, t: String?): View {
                return LayoutInflater.from(context)
                    .inflate(R.layout.flowlayout_item_layout, parent, false).apply {
                        (this as TextView).text = t
                    }
            }
        }
        searchRecord.adapter = localAdapter
        addSetOnClickListener(local_delet) {
            localSearch.clear()
            deleteData(localSearchKey)
            ToastUtil.showToast("全部历史记录已删除！")
            localAdapter.notifyDataChanged()
        }

        searchRecord.setOnTagClickListener { _, position, _ ->
            (activity as SearchActivity).toSearch(localSearch[position])
            false
        }
    }

    override fun initData() {
//        mPresenter?.getData()
    }

    override fun showData(t: HotSearchBean) {
        if (null == t) return
        mBean = t
        everyoneSearch?.adapter =
            object : TagAdapter<String>(mBean?.data?.hotWords) {
                override fun getView(parent: FlowLayout?, position: Int, t: String?): View {
                    return LayoutInflater.from(context)
                        .inflate(R.layout.flowlayout_item_layout, parent, false).apply {
                            (this as TextView).text = t
                        }
                }
            }
    }

}